dojo.provide("uws.drawing.ChildrenManager");

dojo.declare('uws.drawing.ChildrenManager',null,{
	/*
		Adds, edits, gets, and deletes children from the data store for a particular component 
		(specifically ClassBoxes that represents classes).
	*/
	
	idcounter:null,
	components:null,
	
	constructor:function(start){
		this.idcounter = start;
		this.components = [];
	},
	newItem:function(parentId, parentType, type, childType, modifier, name, parameters){
		dataStore.fetchItemByIdentity({
			identity: parentId,
			onItem:function(item){
				dataStore.newItem(
					{
						id: "children_" + (childrenManager.idcounter), 
						type: type,
						parentType: parentType, 
						childType: childType, 
						modifier: modifier, 
						name: name,
						parameters: parameters
					},
					{
						parent: item,
						attribute: 'comp_children'
					}
				);
			}
		});
		
		dataStore.setValue(root,"comp_children_counter",childrenManager.idcounter+1);
		return "children_"+(this.idcounter++);
	},
	addItem:function(id,component){
		this.components[id] = component;
	},
	getById:function(id){
		return this.components[id];
	},
	destroyItem:function(id){
		console.debug("Destroy: "+id);
		dataStore.fetchItemByIdentity({
			identity: id,
			onItem: function(item){
				if(!dataStore.deleteItem(item)){
					console.debug("ConnectionManager: Failed to delete.");
				}
				
			},
			onError:function(e){
				console.debug(e);
			}
		});
	},
	showNewChildrenDialog:function(id){
		var newChildrenDialog = new dijit.Dialog({
			id: 'newChildrenDialog',
			title: "Add a new children",
			style: "width: 350px"
		});
		
		newChildrenDialog.attr("content",
			'<form id="newChildrenForm" action="" onsubmit="return false">'+
				'<br />'+
				'<input type="hidden" name="componentId" value="'+id+'" >' +
				'<table style="width:100%">'+
					'<tr><td class="right">Type</td>'+
						'<td>'+
							'<select name="type" onchange="childrenManager.changeType(\'newChildDialog\')">'+
								'<option value="function">Function</option>'+
								'<option value="variable">Variable</option>'+
							'</select>'+
						'</td>'+
					'</tr>' +
					'<tr><td class="right">Modifier</td>'+
						'<td>'+
							'<select name="modifier">'+
								'<option value=""></option>'+
								'<option value="private">Private</option>'+
								'<option value="protected">Protected</option>'+
								'<option value="public">Public</option>'+
							'</select>'+
						'</td>'+
					'</tr>' +
					'<tr><td class="right">Parameters</td>'+
						'<td><input type="text" name="parameters" id="newChildParameter" /></td>'+
					'</tr>' +
					'<tr><td class="right">Name</td>'+
						'<td><input type="text" name="name" /></td>'+
					'</tr>' +
					'<tr><td class="right" id="newChildReturnTypeLabel">Return Type</td>'+
						'<td><input type="text" name="return_type" /></td>'+
					'</tr>' +
				'</table><br />' +
				'<div class="center">'+
					'<button type="button" onclick="childrenManager.executeNewChild()">' +
						'OK' +
					'</button>' +
					'<button type="button" onclick="dijit.byId(\'newChildrenDialog\').destroy();">' +
						'Cancel' +
					'</button><br />'+
					'<span id="newChildError"></span>'+
				'</div>'+
			'</form>'
		);
	
		newChildrenDialog.show();
	},
	showEditChildrenDialog:function(childId, parentId){
		dataStore.fetchItemByIdentity({
			identity: childId,
			onItem: function(item){
				var editChildrenDialog = new dijit.Dialog({
					id: 'editChildrenDialog',
					title: "Edit",
					style: "width: 350px"
				});
				
				editChildrenDialog.attr("content",
					'<form id="editChildrenForm" action="" onsubmit="return false">'+
						'<br />'+
						'<input type="hidden" name="childId" value="'+ dataStore.getValue(item,"id") +'" >' +
						'<table style="width:100%">'+
							'<tr><td class="right">Type</td>'+
								'<td>'+
									'<select name="type" id="editChildType" onchange="childrenManager.changeType(\'editChildDialog\')">'+
										'<option value="function">Function</option>'+
										'<option value="variable">Variable</option>'+
									'</select>'+
								'</td>'+
							'</tr>' +
							'<tr><td class="right">Modifier</td>'+
								'<td>'+
									'<select name="modifier" id="editChildModifier">'+
										'<option value=""></option>'+
										'<option value="private">Private</option>'+
										'<option value="protected">Protected</option>'+
										'<option value="public">Public</option>'+
									'</select>'+
								'</td>'+
							'</tr>' +
							'<tr><td class="right">Parameters</td>'+
								'<td><input type="text" name="parameters" id="editChildParameter" value="'+dataStore.getValue(item,"parameters")+'" /></td>'+
							'</tr>' +
							'<tr><td class="right">Name</td>'+
								'<td><input type="text" name="name" value="'+dataStore.getValue(item,"name")+'" /></td>'+
							'</tr>' +
							'<tr><td class="right" id="editChildReturnTypeLabel">Return Type</td>'+
								'<td><input type="text" name="return_type" value="'+dataStore.getValue(item,"childType")+'" /></td>'+
							'</tr>' +
						'</table><br />' +
						'<div class="center">'+
							'<button type="button" onclick="childrenManager.executeEditChild()">' +
								'OK' +
							'</button>' +
							'<button type="button" onclick="dijit.byId(\'editChildrenDialog\').destroy();">' +
								'Cancel' +
							'</button><br />'+
							'<span id="editChildError"></span>'+
						'</div>'+
					'</form>'
				);
				
				var type = dataStore.getValue(item, "type");
				var modifier = dataStore.getValue(item, "modifier");
				
				if(type=="variable"){
					dojo.byId("editChildType").selectedIndex=1;
				}
				
				if(modifier=="private"){
					dojo.byId("editChildModifier").selectedIndex=1;
				}else if(modifier=="protected"){
					dojo.byId("editChildModifier").selectedIndex=2;
				}else if(modifier=="public"){
					dojo.byId("editChildModifier").selectedIndex=3;
				}
				
				editChildrenDialog.show();
			}
		});
	},
	executeNewChild:function(){
		var form = dojo.byId('newChildrenForm');
		var parentId= form.componentId.value;
		var type= form.type.value;
		var modifier= form.modifier.value;
		var name= form.name.value;
		var returnType= form.return_type.value;
		var errors = [];
		
		if(type==""){
			errors[0] = "Type";
		}
		
		if(name==""){
			errors[errors.length] = "Name";
		}
		
		if(returnType==""){
			errors[errors.length] = "Type/Return Type";
		}
		
		if(errors.length>0){
			dojo.byId("newChildError").innerHTML = "Please provide the values for: " + errors.join();
			return;
		}
		
		if(type=="function"){
			var parameters = form.parameters.value;
			
			var id = childrenManager.newItem(parentId, "classBox", type, returnType, modifier, name, parameters);
		}else if(type=="variable"){
			var id = childrenManager.newItem(parentId, "classBox", type, returnType, modifier, name, "");
		}
		
		dijit.byId('newChildrenDialog').destroy();
		componentFocusManager._publishProperties();
		var parentId = propertiesManager.currentItem;
		var parent = componentManager.getById(parentId);
		parent.drawChildren();
	},
	executeEditChild:function(){
		var form = dojo.byId('editChildrenForm');
		var id= form.childId.value;
		var type= form.type.value;
		var parameters= form.parameters.value;
		var modifier= form.modifier.value;
		var name= form.name.value;
		var returnType= form.return_type.value;
		var errors = [];
		
		if(type==""){
			errors[0] = "Type";
		}
		
		if(name==""){
			errors[errors.length] = "Name";
		}
		
		if(returnType==""){
			errors[errors.length] = "Type/Return Type";
		}
		
		if(errors.length>0){
			dojo.byId("editChildError").innerHTML = "Please provide the values for: " + errors.join();
			return;
		}
		
		dataStore.fetchItemByIdentity({
			identity: id,
			onItem: function(item){
				dataStore.setValue(item, "type", type);
				dataStore.setValue(item, "childType", returnType);
				dataStore.setValue(item, "modifier", modifier);
				dataStore.setValue(item, "name", name);
				if(type=="function"){
					dataStore.setValue(item, "parameters", parameters);
				}else{
					dataStore.setValue(item, "parameters", "");
				}
			}
		});
		
		dijit.byId('editChildrenDialog').destroy();
		componentFocusManager._publishProperties();
		var parentId = propertiesManager.currentItem;
		var parent = componentManager.getById(parentId);
		parent.drawChildren();
	},
	changeType:function(dialogId){
		if(dialogId=="newChildDialog"){
			var form = dojo.byId('newChildrenForm');
			var type= form.type.value;
			if(type=="function"){
				dojo.byId("newChildParameter").disabled="";
				dojo.byId("newChildReturnTypeLabel").innerHTML="Return Type";
			}else if(type="variable"){
				dojo.byId("newChildParameter").value="";
				dojo.byId("newChildParameter").disabled="disabled";
				dojo.byId("newChildReturnTypeLabel").innerHTML="Variable Type";
			}	
		}else if(dialogId=="editChildDialog"){
			var form = dojo.byId('editChildrenForm');
			var type= form.type.value;
			if(type=="function"){
				dojo.byId("editChildParameter").disabled="";
				dojo.byId("editChildReturnTypeLabel").innerHTML="Return Type";
			}else if(type=="variable"){
				dojo.byId("editChildParameter").value="";
				dojo.byId("editChildParameter").disabled="disabled";
				dojo.byId("editChildReturnTypeLabel").innerHTML="Variable Type";
			}
		}
	},
	deleteChild:function(id){
		if(confirm("Are you sure?")){
			dataStore.fetchItemByIdentity({
				identity: id,
				onItem: function(item){
					if(!dataStore.deleteItem(item)){
						console.debug("ChildrenManager: Failed to delete.");
					}else{
						componentFocusManager._publishProperties();
						var parentId = propertiesManager.currentItem;
						var parent = componentManager.getById(parentId);
						parent.drawChildren();
					}
				}
			});
		}
	}
});